import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  // base: '/hms/',
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/personalCenter',
    component: Layout,
    redirect: '/personalCenter/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/personalCenter/index'),
        name: '个人中心'
        // meta: { title: '个人中心', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '系统首页',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '系统首页', icon: 'dashboard' }
    }]
  }
]

export const asyncRoutes = [
  {
    path: '/usersManagement',
    component: Layout,
    name: '用户管理',
    meta: {
      title: '用户管理',
      icon: 'user',
      roles: ['supperAdmin', 'admin']
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/usersManagement/user/index'),
        name: '用户管理',
        // alwaysShow: true,
        meta: { title: '用户管理' }
      },
      {
        path: 'seller',
        component: () => import('@/views/usersManagement/seller/index'),
        name: '商家管理',
        meta: { title: '商家管理' }
      },
      {
        path: 'delGoods',
        component: () => import('@/views/usersManagement/seller/delGoods'),
        name: '查封商品',
        meta: { title: '查封商品' },
        hidden: true
      }
    ]
  },

  {
    path: '/commentManagement',
    component: Layout,
    meta: {
      roles: ['supperAdmin', 'admin']
    },
    children: [{
      path: 'comment',
      name: '评论管理',
      component: () => import('@/views/commentManagement/comment/index'),
      meta: { title: '评论管理', icon: 'comment' }
    }]
  },

  {
    path: '/analyticStatistics',
    component: Layout,
    name: '数据统计',
    meta: {
      title: '数据统计',
      icon: 'data',
      roles: ['supperAdmin', 'admin']
    },
    children: [
      {
        path: 'userCount',
        name: '用户统计',
        component: () => import('@/views/analyticStatistics/userCount/index'),
        meta: { title: '用户统计' }
      },
      {
        path: 'ordersCount',
        name: '订单统计',
        component: () => import('@/views/analyticStatistics/ordersCount/index'),
        meta: { title: '订单统计' }
      }
    ]
  },

  {
    path: '/shopManagement',
    component: Layout,
    meta: {
      roles: ['supperAdmin', 'seller']
    },
    children: [{
      path: 'shop',
      name: '商店管理',
      component: () => import('@/views/shopManagement/shop/index'),
      meta: { title: '商店管理', icon: 'shop' }
    }]
  },

  {
    path: '/goodsManagement',
    component: Layout,
    meta: {
      roles: ['supperAdmin', 'seller']
    },
    children: [{
      path: 'goods',
      name: '商品管理',
      component: () => import('@/views/goodsManagement/goods/index'),
      meta: { title: '商品管理', icon: 'goods' }
    }]
  },

  {
    path: '/reportStatistics',
    component: Layout,
    name: '报表统计',
    meta: {
      title: '报表统计',
      icon: 'data',
      roles: ['supperAdmin', 'seller']
    },
    children: [
      {
        path: 'personalOrdersCount',
        name: '商家订单统计',
        component: () => import('@/views/reportStatistics/personalOrdersCount/index'),
        meta: { title: '商家订单统计' }
      },
      {
        path: 'commentCount',
        name: '商家评论查询',
        component: () => import('@/views/reportStatistics/commentCount/index'),
        meta: { title: '商家评论查询' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  // mode: browserHistory,
  // base: '/vue/',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
